#include "../lvgl/lvgl.h"
#include <stdio.h>

static lv_obj_t * kb = NULL;
static lv_obj_t * ta_ID = NULL;
static lv_obj_t * ta_Passwd = NULL;

static int tmp_x = 0 ;
static int tmp_y = 0 ;
static int tmp_ID_x = 0 ;
static int tmp_ID_y = 0 ;


static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta = lv_event_get_target(e);

    if(code == LV_EVENT_FOCUSED) {

        lv_keyboard_set_textarea(kb, ta);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);

        tmp_x = lv_obj_get_x( ta_Passwd ) ;
        tmp_y = lv_obj_get_y( ta_Passwd ) ;
        tmp_ID_x = lv_obj_get_x( ta_ID ) ;
        tmp_ID_y = lv_obj_get_y( ta_ID ) ;
        // 设置输入框悬浮于键盘的上方
        lv_obj_align_to(  ta_Passwd , kb ,  LV_ALIGN_OUT_TOP_MID , 0 , -40 );
        lv_obj_align_to(  ta_ID , ta ,  LV_ALIGN_OUT_TOP_MID , 0 , -40 );
    }

    if(code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

        lv_obj_set_x( ta_Passwd , tmp_x );
        lv_obj_set_y( ta_Passwd , tmp_y );
        lv_obj_set_x( ta_ID , tmp_ID_x );
        lv_obj_set_y( ta_ID , tmp_ID_y );

    }
}

void LongInFunc(lv_event_t * e)
{
    char ID[32] = "13414017189";
    char Passwd[32] = "Lj15913931387";
    char Msg[128] ="ID error or Passwd error" ;

    if(strcmp( ID , lv_textarea_get_text( ta_ID )) == 0 && strcmp( Passwd , lv_textarea_get_text( ta_Passwd )) == 0)
    {
        Imgdir_playback();
    }
    else
    {
        static const char * btns[] = {"Apply", "Close", ""};

        lv_obj_t * mbox1 = lv_msgbox_create(NULL, "Error", Msg , btns , true);
        lv_obj_center(mbox1);
    }
    
}

void key_board()
{
    lv_obj_set_style_text_font(lv_scr_act() , &lv_font_montserrat_32 , 0 );

    
    ta_ID = lv_textarea_create(lv_scr_act());
    lv_obj_align(ta_ID, LV_ALIGN_CENTER , 0, -100);
    lv_textarea_set_one_line(ta_ID , true );
    lv_textarea_set_placeholder_text(ta_ID, "ID");
    lv_obj_set_size(ta_ID, 400, 50);
    lv_obj_add_event_cb(ta_ID, ta_event_cb, LV_EVENT_ALL, kb);
    

    ta_Passwd = lv_textarea_create(lv_scr_act());
    lv_obj_align_to(ta_Passwd, ta_ID , LV_ALIGN_OUT_BOTTOM_MID , -70, 50);
    lv_textarea_set_one_line(ta_Passwd , true );
    lv_textarea_set_password_mode(ta_Passwd , true );
    lv_textarea_set_placeholder_text(ta_Passwd, "PassWord");
    lv_obj_set_size(ta_Passwd, 400, 50);
    lv_obj_add_event_cb(ta_Passwd, ta_event_cb, LV_EVENT_ALL, kb);

    lv_obj_t * btn_LogIn =  lv_btn_create(  lv_scr_act() );
    lv_obj_align_to(  btn_LogIn , ta_Passwd ,  LV_ALIGN_OUT_BOTTOM_MID , -100 , 60 );
    lv_obj_set_size( btn_LogIn , 200 , 50 );
    lv_obj_add_event_cb( btn_LogIn ,  LongInFunc ,  LV_EVENT_CLICKED , NULL );


    lv_obj_t * label = lv_label_create ( btn_LogIn );
    lv_obj_center( label );
    lv_label_set_text(  label , "Lon In" );

    kb = lv_keyboard_create(lv_scr_act());
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

}